class CookieUtils {

    /**
     * 根据name获取cookie
     * @param {string} name cookieName
     * @returns {string} cookie
     */
    static get(name) {
        const cookieName = `${encodeURIComponent(name)}=`,
            cookieStart = document.cookie.indexOf(cookieName);
        let cookieValue = null

        if (cookieStart !== -1) {
            let cookieEnd = document.cookie.indexOf(";", cookieStart)

            if (cookieEnd === -1) {
                cookieEnd = document.cookie.length
            }

            cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
        }

        return cookieValue
    }

    /**
     * 设置cookie
     * @param {string} name cookie名
     * @param {string} value cookie值 name=value
     * @param {Date} expires cookie到期时间
     * @param {string} path cookie路径
     * @param {string} domain cookie域
     * @param {boolean} secure 是否加密，如果加密仅ssl携带cookie
     */
    static set(name, value, expires, path, domain, secure) {

        let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`

        if (expires && (expires instanceof Date)) {
            cookieText += `;expires="${expires.toUTCString()}`
        }

        if (path) {
            cookieText += `;path=${path}`
        }

        if (domain) {
            cookieText += `;domain=${domain}`
        }

        if (secure) {
            cookieText += ";secure"
        }

        document.cookie = cookieText
    }

    static unset(name, path, domain, secure) {
        CookieUtils.set(name, "", new Date(0), path, domain, secure)
    }
}